<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\StuffDB;
use App\DBs\WayDB;
use App\DBs\WayStuffDB;
use App\DBs\WayUserDB;
use App\Utilitys\FFbase;
use App\Utilitys\Logs;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class StuffController extends BaseController
{
    protected $log;
    protected $ff;
    protected $db;
    protected $wu;
    protected $way;

    public function __construct()
    {
        parent::__construct();
        $this->db = new StuffDB();
        $this->wu = new WayUserDB();
        $this->way = new WayDB();
        $this->ff = new FFbase();
        $this->log = new Logs();
    }

    /**
     * 素材
     */
    public function getIndex(Request $request)
    {
        $stamp = $this->getUserStamp();
        if (!$stamp) { return $this->toAppJson(-4); }
        $page = $request->post('page',0);
        $size = $request->post('size',10);
        $pageStart = $page * $size;
        $type = $request->post('type',0);
        $resDb = $this->db->getAppIndex($pageStart,$size,$stamp,$type);
        if ($resDb['code']!=0) { return $this->toAppJson(-3); }
        $items = $resDb['data'];
        return $this->toAppJson(0,$items);
    }

    /**
     * 上传图片
     */
    public function setUploadImage(Request $request)
    {
        ini_set("max_execution_time",0); //不限制执行的时间
        $this->log->setAppUpload('图片上传开始：-----');
        $this->log->setAppUpload($request->all());
        $s = $request->post('s','');
        $ss = $this->getParams($s);
        $this->log->setAppUpload($ss);
        $file = $request->file('image');
        $this->log->setAppUpload($file->getClientSize());
        if (!$ss || !$file) {
            return $this->toAppJson(-2);
        }
        $stamp = $ss['stamp'];
        $cw = isset($ss['cw']) ? $ss['cw'] : 100;
        $ch = isset($ss['ch']) ? $ss['ch'] : 200;
        $cw2 = ceil($cw/3); $ch2 = ceil($ch/3);
        $this->log->setAppUpload($stamp);
        $resPath = Upload::uploadFile($file);
        $this->log->setAppUpload($resPath);
        if ($resPath['code']!=0) {
            return $this->toAppJson(-8);
        }
        $filePath = $resPath['data']['filePath'];
        //获取缩略图
        $resFF = $this->ff->setImgToSize($filePath,$cw2,$ch2);
        $this->log->setAppUpload($resFF);
        if ($resFF['code']!=0) {
            return $this->toAppJson(-10);
        }
        $cover = $resFF['data'];
        $resDb = $this->db->setAppAdd($stamp,1,$cover,$filePath);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-500);
        }
        $this->log->setAppUpload($resDb);
        $this->log->setAppUpload('图片上传结束：-----');
        return $this->toAppJson(0);
    }

    /**
     * 拆解参数
     */
    public function getParams($s)
    {
        if (!$s) { return '' ;}
        $res = explode('&',$s);
        $items = array();
        foreach ($res as $re) {
            $rs = explode('=',$re);
            $items[$rs[0]] = $rs[1];
        }
        return $items;
    }

    /**
     * 上传视频
     */
    public function setUploadVideo(Request $request)
    {
        ini_set("max_execution_time",0); //不限制执行的时间
        $this->log->setAppUpload('视频上传开始：-----');
        $this->log->setAppUpload($request->all());
        $s = $request->post('s','');
        $ss = $this->getParams($s);
        $this->log->setAppUpload($ss);
        $file = $request->file('video');
        $this->log->setAppUpload($file->getClientSize());
        if (!$ss || !$file) {
            return $this->toAppJson(-2);
        }
        $stamp = $ss['stamp'];
        $cw = isset($ss['cw']) ? $ss['cw'] : 100;
        $ch = isset($ss['ch']) ? $ss['ch'] : 200;
        $cw2 = ceil($cw/3); $ch2 = ceil($ch/3);
        $this->log->setAppUpload($stamp);
        $resPath = Upload::uploadFile($file);
        $this->log->setAppUpload($resPath);
        if ($resPath['code']!=0) {
            return $this->toAppJson(-8);
        }
        $filePath = $resPath['data']['filePath'];
        //获取GIF
        $resFF = $this->ff->setMp4ToGif($filePath,$cw2,$ch2);
        $this->log->setAppUpload($resFF);
        if ($resFF['code']!=0) {
            return $this->toAppJson(-10);
        }
        $cover = $resFF['data'];
        $resDb = $this->db->setAppAdd($stamp,2,$cover,$filePath);
        $this->log->setAppUpload($resDb);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-500);
        }
        $this->log->setAppUpload($resDb);
        $this->log->setAppUpload('视频上传结束：-----');
        return $this->toAppJson(0);
    }

    /**
     * 上传语音
     */
    public function setUploadAudio(Request $request)
    {
        ini_set("max_execution_time",0); //不限制执行的时间
        $this->log->setAppUpload('语音上传开始：-----');
        $this->log->setAppUpload($request->all());
        $s = $request->post('s','');
        $ss = $this->getParams($s);
        $this->log->setAppUpload($ss);
        $file = $request->file('audio');
        $this->log->setAppUpload($file->getClientSize());
        if (!$ss || !$file) {
            return $this->toAppJson(-2);
        }
        $stamp = $ss['stamp'];
        $this->log->setAppUpload($stamp);
        $resPath = Upload::uploadFile($file);
        $this->log->setAppUpload($resPath);
        if ($resPath['code']!=0) {
            return $this->toAppJson(-8);
        }
        $filePath = $resPath['data']['filePath'];
        //获取GIF
        $resFF = $this->ff->setAmrToMp3($filePath);
        $this->log->setAppUpload($resFF);
        if ($resFF['code']!=0) {
            return $this->toAppJson(-10);
        }
        $cover = $resFF['data'];
        $resDb = $this->db->setAppAdd($stamp,3,$cover,$filePath);
        $this->log->setAppUpload($resDb);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-500);
        }
        $this->log->setAppUpload($resDb);
        $this->log->setAppUpload('语音上传结束：-----');
        return $this->toAppJson(0);
    }
}